var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    address: {
      id: 0,
      provinceId: 0,
      cityId: 0,
      districtId: 0,
      address: '',
      full_region: '',
      name: '',
      mobile: '',
      isdefault: 0
    },
    addressId: 0,
    openSelectRegion: false,
    selectRegionList: [
      { id: 0, name: '省份', parent_id: 1, type: 1 },
      { id: 0, name: '城市', parent_id: 1, type: 2 },
      { id: 0, name: '区县', parent_id: 1, type: 3 }
    ],
    regionType: 1,
    regionList: [],
    selectRegionDone: false
  },

  //手机号
  bindinputMobile(event) {
    let address = this.data.address;
    address.mobile = event.detail.value;
    this.setData({
      address: address
    });
  },
  //姓名
  bindinputName(event) {
    let address = this.data.address;
    address.name = event.detail.value;
    this.setData({
      address: address
    });
  },
  //详细地址
  bindinputAddress(event) {
    let address = this.data.address;
    address.address = event.detail.value;
    this.setData({
      address: address
    });
  },
  //默认地址
  bindIsDefault() {
    let address = this.data.address;
    address.isDefault = !address.isDefault;
    this.setData({
      address: address
    });
  },
  //查看地址
  getAddressDetail() {
    let that = this;
    util.request(api.AddressDetail, { id: that.data.addressId }).then(function (res) {
      if (res.code === 0) {
        if (res.data.isDefault === 1) {
          res.data.isDefault = true
        }
        that.setData({
          address: res.data
        });
      }
    });
  },

  setRegionDoneStatus() {
    let that = this;
    let doneStatus = that.data.selectRegionList.every(item => {
      return item.id != 0;
    });
    that.setData({
      selectRegionDone: doneStatus
    })
  },

  //点击时地址三级联动显示和隐藏
  chooseRegion() {
    let that = this;
    this.setData({
      openSelectRegion: !this.data.openSelectRegion
    });

    //设置区域选择数据
    let address = this.data.address;
    //绑定数据
    if (address.provinceId > 0 && address.cityId > 0 && address.districtId > 0) {
      let selectRegionList = this.data.selectRegionList;
      selectRegionList[0].id = address.provinceId;
      selectRegionList[0].name = address.provinceName;
      selectRegionList[0].parent_id = 1;

      selectRegionList[1].id = address.cityId;
      selectRegionList[1].name = address.cityName;
      selectRegionList[1].parent_id = address.provinceId;

      selectRegionList[2].id = address.districtId;
      selectRegionList[2].name = address.areaName;
      selectRegionList[2].parent_id = address.cityId;

      this.setData({
        selectRegionList: selectRegionList,
        regionType: 3
      });
      this.getRegionList(0);
    } else {
      this.setData({
        selectRegionList: [
          { id: 0, name: '省份', parent_id: 1, type: 1 },
          { id: 0, name: '城市', parent_id: 1, type: 2 },
          { id: 0, name: '区县', parent_id: 1, type: 3 }
        ],
        regionType: 1
      })
      //重新查询
      this.getRegionList(0);
    }
    this.setRegionDoneStatus();

  },
  // 页面初始化 options为页面跳转所带来的参数
  onLoad: function (options) {
    console.log(options)
    if (options.id) {
      this.setData({
        addressId: options.id
      });
      this.getAddressDetail();
    }
    this.getRegionList(0);
  },
  onReady: function () {
  },
  //判断信息方法
  selectRegionType(event) {
    let that = this;
    let regionTypeIndex = event.target.dataset.regionTypeIndex;
    let selectRegionList = that.data.selectRegionList;

    //判断是否可点击
    if (regionTypeIndex + 1 == this.data.regionType || (regionTypeIndex - 1 >= 0 && selectRegionList[regionTypeIndex - 1].id <= 0)) {
      return false;
    }

    this.setData({
      regionType: regionTypeIndex + 1
    })

    let selectRegionItem = selectRegionList[regionTypeIndex];
    //根据id？？？
    this.getRegionList(selectRegionItem.id);

    this.setRegionDoneStatus();

  },
  //根据选择显示三级联动数据
  selectRegion(event) {
    let that = this;
    let regionIndex = event.target.dataset.regionIndex;
    let regionItem = this.data.regionList[regionIndex];
    let regionType = regionItem.layer;
    let selectRegionList = this.data.selectRegionList;
    selectRegionList[regionType - 1] = regionItem;
    if (regionType != 3) {
      this.setData({
        selectRegionList: selectRegionList,
        regionType: regionType + 1
      })
      this.getRegionList(regionItem.areaCode);
    } else {
      this.setData({
        selectRegionList: selectRegionList
      })
    }
    //重置下级区域为空
    selectRegionList.map((item, index) => {
      if (index > regionType - 1) {
        item.id = 0;
        item.name = index == 1 ? '城市' : '区县';
        item.parent_id = 0;
      }
      return item;
    });
    this.setData({
      selectRegionList: selectRegionList
    })
    let regions = that.data.regionList;
    regions.forEach(function (item, index) {
      //标记已选择的   
      if (regions.areaCode == item.areaCode && that.data.selectRegionList[that.data.regionType - 1].id == item.parent_id) {

        item.selected = true;
      } else {

        item.selected = false;
      }
      return item;
    })
    that.setData({
      regionList: regions
    });

    this.setRegionDoneStatus();

  },
  //点击确定
  doneSelectRegion() {
    if (this.data.selectRegionDone === false) {
      return false;
    }
    let address = this.data.address;
    let selectRegionList = this.data.selectRegionList;
    address.provinceId = selectRegionList[0].areaCode;
    address.cityId = selectRegionList[1].areaCode;
    address.districtId = selectRegionList[2].areaCode;
    address.province_name = selectRegionList[0].name;
    address.city_name = selectRegionList[1].name;
    address.district_name = selectRegionList[2].name;
    address.full_region = selectRegionList.map(item => {
      return item.name;
    }).join('');

    this.setData({
      address: address,
      openSelectRegion: false
    });

  },

  cancelSelectRegion() {
    this.setData({
      openSelectRegion: false,
      regionType: this.data.regionDoneStatus ? 3 : 1
    });

  },
  //地址初始化
  getRegionList(regionId) {
    let that = this;
    let regionType = that.data.regionType;
    util.request(api.RegionList, { pid: regionId }).then(function (res) {
      let regions = res.data;
      if (res.code === 0) {
        regions.forEach(function (item, index) {
          //标记已选择的   
          if (regionType == item.orderNum && that.data.selectRegionList[regionType - 1].id == item.parentCode) {
            item.selected = true;
          } else {
            item.selected = false;
          }
          return item;
        })
        that.setData({
          regionList: regions
        });
      }
    });
  },
  //取消
  cancelAddress() {
    wx.navigateTo({
      url: '/pages/ucenter/address/address',
    })
  },
  //添加地址
  saveAddress() {
    let address = this.data.address; 
    if (address.name == '') {
      util.showErrorToast('请输入姓名');

      return false;
    }

    if (address.mobile == '') {
      util.showErrorToast('请输入手机号码');
      return false;
    }
    if (address.districtId == 0) {
      util.showErrorToast('请输入省市区');
      return false;
    }

    if (address.address == '') {
      util.showErrorToast('请输入详细地址');
      return false;
    }
    let that = this;
    let isDefault = 0;
    if (address.isDefault) {
      isDefault = 1
    }
    console
    util.request(api.AddressSave, {
      id: address.id,
      name: address.name,
      mobile: address.mobile,
      provinceId: address.provinceId,
      cityId: address.cityId,
      areaId: address.districtId,
      address: address.address,
      isDefault: isDefault,
    }, 'POST').then(function (res) {
      if (res.code === 0) { 
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          image: '/static/images/icon_error.png',
          title: res.msg,
          mask: true
        });
      }
    });
  },

  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
})